package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.ModuleTypeDao;
import com.sugon.entity.ModuleArchivesEntity;
import com.sugon.entity.ModuleTypeEntity;
import com.sugon.modules.mold.consts.MoldState;
import com.sugon.service.ModuleArchivesService;
import com.sugon.service.ModuleTypeService;
import com.sugon.utils.ShiroUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 模具类型Service实现类
 *
 * @author YuXD
 * @date 2019-08-06 16:31:41
 */
@Service("moduleTypeService")
public class ModuleTypeServiceImpl implements ModuleTypeService {

    @Resource
    private ModuleTypeDao moduleTypeDao;
    @Resource
    private ModuleArchivesService moldService;

    @Override
    public List<ModuleTypeEntity> loadComplexMoldType(String cjId, String productModel, List<Integer> excludeStatusList) {
        Map<String, Object> map = new HashMap<>();
        List<ModuleTypeEntity> typeList = queryList(map);
        map.put("subShop", cjId);
        map.put("productModel", productModel);
        map.put("excludeStatusList", excludeStatusList);
        List<ModuleArchivesEntity> moldList = moldService.queryList(map);
        Map<String, List<ModuleArchivesEntity>> moldGroup = moldList.stream().collect(Collectors.groupingBy(ModuleArchivesEntity::getModuleType));
        for (ModuleTypeEntity moldType : typeList) {
            int totalNum = 0;
            long availableNum = 0;
            List<ModuleArchivesEntity> matchedMoldList = moldGroup.get(moldType.getId());
            if (CollectionUtil.isNotEmpty(matchedMoldList)) {
                totalNum = matchedMoldList.size();
                availableNum = matchedMoldList.stream().filter(mold -> !CollectionUtil.newArrayList(MoldState.MUJUBAOFEI.getState(), MoldState.XIANGKUAIBAOFEI.getState(), MoldState.FENGCUN.getState(), MoldState.WAIJIE.getState()).contains(mold.getCurrentState())).count();
            }
            moldType.setTotalNum(totalNum);
            moldType.setAvailableNum(availableNum);
        }

        // 计算每个父类的可用数
        Map<String, List<ModuleTypeEntity>> newGroup = typeList.stream().collect(Collectors.groupingBy(ModuleTypeEntity::getId));
        Map<String, List<ModuleTypeEntity>> moldTypeGroup = typeList.stream().collect(Collectors.groupingBy(ModuleTypeEntity::getSuperType));
        moldTypeGroup.forEach((superType, moldTypeList) -> {
            if (CollectionUtil.isNotEmpty(moldTypeList)) {
                List<ModuleTypeEntity> superMoldTypes = newGroup.get(superType);
                if (CollectionUtil.isNotEmpty(superMoldTypes)) {
                    superMoldTypes.get(0).setTotalNum(moldTypeList.stream().map(ModuleTypeEntity::getTotalNum).reduce(Integer::sum).get());
                    superMoldTypes.get(0).setAvailableNum(moldTypeList.stream().map(ModuleTypeEntity::getAvailableNum).reduce(Long::sum).get());
                }
            }
        });
        return typeList;
    }

    /**
     * 根据模具类型ID查询列表
     *
     * @param moduleTypeId
     * @return
     */
    @Override
    public List<String> getModuleTypeIdList(String moduleTypeId) {
        List<String> mouldSubTypeList = null;
        if (StrUtil.isNotEmpty(moduleTypeId)) {
            mouldSubTypeList = moduleTypeDao.getMouldTypeByParentId(moduleTypeId);
            if (mouldSubTypeList == null) {
                mouldSubTypeList = new ArrayList<>();
            }
            mouldSubTypeList.add(moduleTypeId);
        }
        return mouldSubTypeList;
    }

    /**
     * 对象查询
     *
     * @param id 主键
     * @return
     */
    @Override
    public ModuleTypeEntity queryObject(String id) {
        return moduleTypeDao.queryObject(id);
    }

    /**
     * 列表查询
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<ModuleTypeEntity> queryList(Map<String, Object> map) {
        if (map == null) {
            map = MapUtil.newHashMap();
        }
        return moduleTypeDao.queryList(map);
    }

    /**
     * 查询数
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return moduleTypeDao.queryTotal(map);
    }

    /**
     * 保存
     *
     * @param moduleType 实体
     * @return
     */
    @Override
    public int save(ModuleTypeEntity moduleType) {
        moduleType.setId(RandomUtil.randomString(32));
        moduleType.setDelFlag(1);
        moduleType.setCreateUser(ShiroUtils.getUserId());
        moduleType.setCreateDate(new Date());
        return moduleTypeDao.save(moduleType);
    }

    /**
     * 更新
     *
     * @param moduleType 实体
     * @return
     */
    @Override
    public int update(ModuleTypeEntity moduleType) {
        moduleType.setUpdateUser(ShiroUtils.getUserId());
        moduleType.setUpdateDate(new Date());
        return moduleTypeDao.update(moduleType);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return moduleTypeDao.delete(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return moduleTypeDao.deleteBatch(ids);
    }
}
